package com.mysql.utils;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 作者:wmx
 * 1、功能：MySQL相关连接
 * 2、用途:用于满足项目的功能,实现项目的数据交互等
 * 3、
 * */

public class DBUtil {
    private static String DRIVER = null;
    private static String URL = null;
    private static String USER = null;
    private static String PASSWORD = null;
    public  static Connection conn =null;
    private static Properties properties = new Properties();
    static {
        InputStream inputStream = DBUtil.class.getClassLoader().getResourceAsStream("dp.properties");
        try {

            properties.load(inputStream);
            DRIVER = properties.getProperty("driver");
            URL = properties.getProperty("url");
            USER = properties.getProperty("user");
            PASSWORD = properties.getProperty("password");
            Class.forName(DRIVER);


        } catch (IOException e) {
            e.printStackTrace();
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    public static Connection getConn(){
        try {
            conn = DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  conn;
    };
    public  static void getClose(Connection conn,ResultSet resultSet,Statement statement) throws SQLException {
        if(conn!=null){
            conn.close();
            System.out.println(conn+"关闭成功");
        }else if(resultSet!=null){
            resultSet.close();
            System.out.println(resultSet+"关闭成功");
        }else if(statement!=null){
            statement.close();
            System.out.println(statement+"关闭成功");
        }

    }


}
